
#include "dataprepare.h"
#include "cmdupdatedata.h"
#include "../mainctrl.h"
//___________________________________________________________________
CT_REGISTER_ASYNCCMD(ID_MAINDB_UPDATEDATA , CCmdUpdateData);

//////////////////////////CCmdHeroCfgFromDB
int CCmdUpdateData::Do(void *pData)
{
    int iRet = 0;
    CTMsgHead stMsgHead;
    stMsgHead.shMsgID = ID_MAINDB_UPDATEDATA ;
    stMsgHead.shMsgType = EMT_REQUEST;
    stMsgHead.iCmdSeq = GetCmdSeq();
    stMsgHead.llMsgAct = reinterpret_cast<int64>(this);

    if(m_stReq.pszData == NULL || m_pstMain == NULL )
    {
        CT_WARNING((""));
        return Done(RET_SYSTEM_FAIL);
    }

    CTCodeStream s(m_pstMain->m_stEncodeBuf);
    CodeConvert(s, stMsgHead, NULL, bin_encode());
    CodeConvert(s, m_stReq, NULL, bin_encode());
    if (s.m_iRet)
    {
        CT_WARNING((""));
        return Done(RET_SYSTEM_FAIL);
    }

    if(s.m_ushPos + m_stReq.iDataLen > s.m_ushBufSize)
    {
        CT_ERROR(("update data is too big data len %d pos %d buffLen %d",m_stReq.iDataLen,
            s.m_ushPos,m_stReq.iDataLen));
        return Done(RET_SYSTEM_FAIL);
    }

    memcpy(s.m_pszBuf+s.m_ushPos,m_stReq.pszData,m_stReq.iDataLen);
    s.m_ushPos+=m_stReq.iDataLen;
    
    CT_TRACE(("UPDATE_DATA: RequestToDB s.m_pszBuf=%p, s.m_ushPos=%d CmdID=%d", s.m_pszBuf, s.m_ushPos, stMsgHead.shMsgID));
    iRet = m_pstMain->RequestToDB(s.m_pszBuf, s.m_ushPos);
    if (iRet)
    {
        CT_WARNING((""));
        return Done(RET_SYSTEM_FAIL);
    }

    return iRet;
}

int CCmdUpdateData::OnAnswer(void* pData)
{
    int iRet = RET_OK;
    CT_TRACE(("recv a update answer"));   
    if (pData == NULL)
    {
        return Done(RET_SYSTEM_FAIL);
    }

    CCmdOnAnswerParam* pstParam = (CCmdOnAnswerParam*) pData;
    if((CMainCtrl*)(pstParam->m_pMain) != m_pstMain ||
        pstParam->m_pstMsgHead->shMsgID != ID_MAINDB_UPDATEDATA ||
        pstParam->m_pstMsgHead->shMsgType != EMT_ANSWER)
    {
        return Done(RET_SYSTEM_FAIL);
    }

    iRet = CodeConvert(*pstParam->m_pstCode, m_stAns, NULL, bin_decode());
    if (iRet != 0)
    {
        CT_WARNING(("decode ans error"));

        return Done(iRet);
    }

    if(m_stAns.stResult.iResultID == 0)
    {
        CT_UPDATE(("-,%d,%d,%d,%s,%d", 
            m_stAns.m_stKey.m_chDataType,
            CT_UINT64_HIGH(m_stAns.m_stKey.m_u64ID), 
            CT_UINT64_LOW(m_stAns.m_stKey.m_u64ID),
            m_stAns.m_stKey.m_sStrID(),
            m_stReq.m_iOpType));

        m_pstMain->m_stUpdateMgr.UpdateResponse(m_stAns.m_stKey);
    }
    else
    {
        CT_WARNING(("-,%d,%d,%d,%s,%d  update failed ret %d", 
            m_stAns.m_stKey.m_chDataType,
            CT_UINT64_HIGH(m_stReq.m_stKey.m_u64ID), 
            CT_UINT64_LOW(m_stReq.m_stKey.m_u64ID),
            m_stReq.m_stKey.m_sStrID(),
            m_stReq.m_iOpType,m_stAns.stResult.iResultID));
    }

    return Done(iRet);
}


CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_UPDATE_SNS , CCmdUpdateSnsDetail);
int CCmdUpdateSnsDetail::Do(void* pData)
{
    if (0 == _uiGid)
    {
        CT_ERROR(("%d", _uiGid));
        return Done (RET_INVALID_PARAM);
    }

    LogicDo(RET_OK);

    return RET_OK;
}

int CCmdUpdateSnsDetail::LogicDo (int iRet)
{
    PREPARE_PLAYER_SNS_DETAIL_INFO(this, _uiGid);

    int iIndex = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash.HashFind(_uiGid);
    if (iIndex < 0)
    {
        CT_ERROR(("get user sns detail info error, uid = %u", _uiGid));
        return Done(RET_SYSTEM_FAIL);
    }    
    
    CPlayerSnsDetailInfo & stPlayerSnsDetailInfo = CMainCtrl::m_pstMain->m_stPlayerSnsDetailInfoHash[iIndex];

    switch (_ucType)
    {
    case SNS_DETAIL_FIGHTING:
        {
            if (_iRoleFighting < 0 || _iTeamFighting < 0)
            {
                CT_ERROR(("%d, %d, %d", _uiGid, _iRoleFighting, _iTeamFighting));
                return Done (RET_INVALID_PARAM);
            }

            stPlayerSnsDetailInfo._stCommInfo._iTeamFighting = _iTeamFighting;
            stPlayerSnsDetailInfo._stCommInfo._iRoleFighting = _iRoleFighting;
        }
        break;

    case SNS_DETAIL_TITLE:
        {
            if (0 != _stNewTitle._ucTitle)
            {
                stPlayerSnsDetailInfo._stCommInfo._stTitle.AddNewTitle(_stNewTitle);
                stPlayerSnsDetailInfo._stCommInfo._stTitle.DelExpiredTitle(CMainCtrl::m_pstMain->m_iNow);
            }
            else
            {
                CT_ERROR(("%d, %d, %d", _uiGid, _stNewTitle._ucTitle, _stNewTitle._iExpiredTime));
            }
        }
        break;

    default:
        {
             CT_ERROR(("%d, %d", _uiGid, _ucType));
        }
    }

    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_SNS_DETAIL_INFO;
    stKey.m_u64ID = stPlayerSnsDetailInfo._uiGID;
    CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerSnsDetailInfo, VOP_UPDATE);

     if (SNS_DETAIL_TITLE == _ucType)
     {
         CMainCtrl::m_pstMain->m_stStatusMgr.NotifyUserStatusToMapByGID(_uiGid);
     }

    return Done(RET_OK);
}

